//
//  ChatCellViewFrame.m
//  JiaoX
//
//  Created by yinqy on 15/3/16.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "ChatCellViewFrame.h"
#import "ChatLabel.h"

@implementation ChatCellViewFrame


-(void)setMessage:(JXMessage *)message preTimestamp:(int64_t)preTimestamp{
    _message = message;
    [_message setPreTimestamp:preTimestamp];
    
    CGFloat currentHeight = 15;
    
    if (preTimestamp==0 || (message.timestamp-preTimestamp)>1000*60*2) {
        self.timeLabelFrame = CGRectMake(0, 5, JXScreenW, 20);
        currentHeight +=20;
    }else{
        self.timeLabelFrame = CGRectZero;
    }
    
    if ([Utils isBlankString:message.groupId]) {
        //不是群消息。不显示名称
        self.nameLabelFrame = CGRectZero;
    }else{
        //是群消息。
        CGFloat nameLabelX = 10+HEADIMG_WIDTH;
        CGFloat nameLabelW = JXScreenW-2*nameLabelX;
        CGFloat nameLabelY = currentHeight +2.5;
        CGFloat nameLabelH = NAME_LEBAL_FONT.lineHeight;
        
        self.nameLabelFrame = CGRectMake(nameLabelX, nameLabelY, nameLabelW, nameLabelH);
        
        currentHeight = CGRectGetMaxY(self.nameLabelFrame);
    }
    
    CGFloat headImgY = currentHeight;
    CGFloat headImgX = 0;
    
    CGSize contentSize = CGSizeZero;
    CGFloat maxWidth = JXScreenW-(10+HEADIMG_WIDTH)*2-25;
    if (message.msgType==MsgTypeText) {
        NSString *content = [message actualContent];
        CGSize contentStrSize = [ChatLabel sizeWithContent:content width:maxWidth];
        contentSize = CGSizeMake(contentStrSize.width+25, contentStrSize.height+SCALEX(10));
    }else{
        CGFloat imgW = maxWidth;
        if (message.getImgW<imgW) {
            imgW = message.getImgW;
        }
        CGFloat imgH = message.getImgH*(imgW/message.getImgW);
        contentSize = CGSizeMake(imgW, imgH);
    }
    CGFloat contentLabelX = JXScreenW-(10+HEADIMG_WIDTH)-contentSize.width;
    CGFloat contentLabelY = currentHeight;
    
    if ([CURRENTUSERID isEqualToString:message.fromPeerId]) {
        //发出去的消息
        headImgX = JXScreenW-10-HEADIMG_WIDTH;
    }else{
        //收到的
        headImgX = 10;
        contentLabelX = 10+HEADIMG_WIDTH;
    }
    
    self.contentLabelFrame = (CGRect){{contentLabelX, contentLabelY}, contentSize};
    
    CGSize failedSize = IMG(@"ic_send_failed").size;
    if ([CURRENTUSERID isEqualToString:message.fromPeerId]) {
        //发出去的消息
        self.failedBtnFrame = (CGRect){{CGRectGetMinX(self.contentLabelFrame)-5-failedSize.width,contentLabelY},failedSize};
    }else{
        //收到的
        self.failedBtnFrame = (CGRect){{CGRectGetMaxX(self.contentLabelFrame)+5,contentLabelY},failedSize};
    }
    
    self.headImgFrame = CGRectMake(headImgX, headImgY, HEADIMG_WIDTH, HEADIMG_WIDTH);
    self.frame = CGRectMake(0, 0, JXScreenW, CGRectGetMaxY(self.contentLabelFrame)+10);
}

@end
